package approve.adapter;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import approve.action.App_ApprovedDocument;
import approve.action.App_DeclineDocument;
import approve.action.App_ReferenceDoc_Approved;
import approve.action.Library_Label_Item;
import approve.action.LoginActivity;
import approve.action.My_App_ApproveDocument;
import approve.action.R;
import approve.dao.ListLabelDAO;
import approve.model.Approvedoc;
import approve.model.ListLabel;

public class Library_Adapter_Listlabel extends BaseAdapter{
	Context context;
	ArrayList<ListLabel> lstlabel = new ArrayList<ListLabel>();
	String strid;
	int flagpage;
	String idem;
	ViewHolder holder;
	Approvedoc approvedoc;
	
	public Library_Adapter_Listlabel(App_ApprovedDocument context, ArrayList<ListLabel> lst,String strid,String idem, int flagpage){
		this.context=context;
		this.lstlabel=lst;
		this.flagpage=flagpage;
		this.strid=strid;
		this.idem=idem;
	}
	public Library_Adapter_Listlabel(App_DeclineDocument context, ArrayList<ListLabel> lst,String strid,String idem, int flagpage){
		this.context=context;
		this.lstlabel=lst;
		this.flagpage=flagpage;
		this.strid=strid;
		this.idem=idem;
	}
	public Library_Adapter_Listlabel(App_ReferenceDoc_Approved context, ArrayList<ListLabel> lst,String strid,String idem, int flagpage){
		this.context=context;
		this.lstlabel=lst;
		this.flagpage=flagpage;
		this.strid=strid;
		this.idem=idem;
	}
	public Library_Adapter_Listlabel(My_App_ApproveDocument context, ArrayList<ListLabel> lst,String strid,String idem, int flagpage){
		this.context=context;
		this.lstlabel=lst;
		this.flagpage=flagpage;
		this.strid=strid;
		this.idem=idem;
	}
	public Library_Adapter_Listlabel(Library_Label_Item context, ArrayList<ListLabel> lst,Approvedoc approvedoc,String idem, int flagpage){
		this.context=context;
		this.lstlabel=lst;
		this.flagpage=flagpage;
		this.approvedoc = approvedoc;
		this.idem=idem;
	}
	@Override
	public int getCount() {
		return lstlabel.size();
	}

	@Override
	public Object getItem(int arg0) {
		return lstlabel.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ListLabel onelabel = lstlabel.get(position);
		
		if(convertView==null){
			holder = new ViewHolder();
			
			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView=inflater.inflate(R.layout.library_listview_adapter, parent,false);
			
			holder.imageView=(ImageView)convertView.findViewById(R.id.imageView1);
			holder.tvlabelname=(TextView)convertView.findViewById(R.id.tvlabelname);
			holder.lila_adapter_listlabel=(LinearLayout)convertView.findViewById(R.id.lila_adapter_listlabel);
			convertView.setBackgroundResource(R.drawable.list_selector);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder)convertView.getTag();
		}
		holder.tvlabelname.setText(onelabel.getName_lable());
		holder.lila_adapter_listlabel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				switch (flagpage) {
				case 1:
					AlertDialog.Builder builder = new AlertDialog.Builder(((App_ApprovedDocument)context));
					builder.setTitle(LoginActivity.lang.getString("move"));
					builder.setMessage(LoginActivity.lang.getString("areyousure")+" ?");
					builder.setCancelable(false);
					builder.setPositiveButton(LoginActivity.lang.getString("ok"), new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							((App_ApprovedDocument)context).getAlertwaiting1().show();
							try{
								new Thread(){
									public void run() {
										ListLabelDAO lstdao = new ListLabelDAO();
										lstdao.moveclick(LoginActivity.database, onelabel.getId(), strid, idem);
										((App_ApprovedDocument)context).getUiHandler1().post( new Runnable(){
											@Override
											public void run() {
												if(((App_ApprovedDocument)context).isUpdateRequired1()){
												}else{
													((App_ApprovedDocument)context).loadListView();
													((App_ApprovedDocument)context).getAlert_pop1().cancel();
													((App_ApprovedDocument)context).setFlagviewpop(0);
													((App_ApprovedDocument)context).getAlertwaiting1().cancel();		                               
												}
											}
										} );
									}
								}.start();
							}catch (Exception e) {}
						}
						
					});
					builder.setNegativeButton(LoginActivity.lang.getString("cancel"), new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {

						}
					});
					AlertDialog alertDialog = builder.create();
					alertDialog.show();
					break;
				case 2:
					AlertDialog.Builder builder2 = new AlertDialog.Builder(((App_DeclineDocument)context));
					builder2.setTitle(LoginActivity.lang.getString("move"));
					builder2.setMessage(LoginActivity.lang.getString("areyousure")+" ?");
					builder2.setCancelable(false);
					builder2.setPositiveButton(LoginActivity.lang.getString("ok"), new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							((App_DeclineDocument)context).getAlertwaiting2().show();
							try{
								new Thread(){
									public void run() {
										ListLabelDAO lstdao = new ListLabelDAO();
										lstdao.moveclick(LoginActivity.database, onelabel.getId(), strid, idem);
										((App_DeclineDocument)context).getUiHandler2().post( new Runnable(){
											@Override
											public void run() {
												if(((App_DeclineDocument)context).isUpdateRequired2()){
												}else{
													((App_DeclineDocument)context).loadListView();
													((App_DeclineDocument)context).getAlert_pop2().cancel();
													((App_DeclineDocument)context).setFlagviewpop(0);
													((App_DeclineDocument)context).getAlertwaiting2().cancel();		                               
												}
											}
										} );
									}
								}.start();
							}catch (Exception e) {}
						}
						
					});
					builder2.setNegativeButton(LoginActivity.lang.getString("cancel"), new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {

						}
					});
					AlertDialog alertDialog2 = builder2.create();
					alertDialog2.show();
					break;
				case 3:
					AlertDialog.Builder builder3 = new AlertDialog.Builder(((App_ReferenceDoc_Approved)context));
					builder3.setTitle(LoginActivity.lang.getString("move"));
					builder3.setMessage(LoginActivity.lang.getString("areyousure")+" ?");
					builder3.setCancelable(false);
					builder3.setPositiveButton(LoginActivity.lang.getString("ok"), new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							((App_ReferenceDoc_Approved)context).getAlertwaiting2().show();
							try{
								new Thread(){
									public void run() {
										ListLabelDAO lstdao = new ListLabelDAO();
										lstdao.moverefer(LoginActivity.database, idem, onelabel.getId(), strid);
										((App_ReferenceDoc_Approved)context).getUiHandler().post( new Runnable(){
											@Override
											public void run() {
												if(((App_ReferenceDoc_Approved)context).isUpdateRequired()){
												}else{
													((App_ReferenceDoc_Approved)context).loadlistview();
													((App_ReferenceDoc_Approved)context).getAlert_pop2().cancel();
													((App_ReferenceDoc_Approved)context).setFlagviewpop(0);
													((App_ReferenceDoc_Approved)context).getAlertwaiting2().cancel();		                               
												}
											}
										} );
									}
								}.start();
							}catch (Exception e) {}
						}
						
					});
					builder3.setNegativeButton(LoginActivity.lang.getString("cancel"), new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							
						}
					});
					AlertDialog alertDialog3 = builder3.create();
					alertDialog3.show();
					
					break;
				case 4:
					AlertDialog.Builder builder4 = new AlertDialog.Builder(((My_App_ApproveDocument)context));
					builder4.setTitle(LoginActivity.lang.getString("move"));
					builder4.setMessage(LoginActivity.lang.getString("areyousure")+" ?");
					builder4.setCancelable(false);
					builder4.setPositiveButton(LoginActivity.lang.getString("ok"), new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							((My_App_ApproveDocument)context).getAlertwaiting1().show();
							try{
								new Thread(){
									public void run() {
										ListLabelDAO lstdao = new ListLabelDAO();
										lstdao.moveMyApproveDoc(LoginActivity.database, idem, onelabel.getId(), strid);
										((My_App_ApproveDocument)context).getUiHandler1().post( new Runnable(){
											@Override
											public void run() {
												if(((My_App_ApproveDocument)context).isUpdateRequired1()){
												}else{
													((My_App_ApproveDocument)context).loadlistview();
													((My_App_ApproveDocument)context).getAlert_pop1().cancel();
													((My_App_ApproveDocument)context).setFlagviewpop(0);
													((My_App_ApproveDocument)context).getAlertwaiting1().cancel();		                               
												}
											}
										} );
									}
								}.start();
							}catch (Exception e) {}
						}
						
					});
					builder4.setNegativeButton(LoginActivity.lang.getString("cancel"), new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {

						}
					});
					AlertDialog alertDialog4 = builder4.create();
					alertDialog4.show();
					break;
					
				case 5:
					AlertDialog.Builder builder5 = new AlertDialog.Builder(((Library_Label_Item)context));
					builder5.setTitle(LoginActivity.lang.getString("move"));
					builder5.setMessage(LoginActivity.lang.getString("areyousure")+" ?");
					builder5.setCancelable(false);
					builder5.setPositiveButton(LoginActivity.lang.getString("ok"), new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							((Library_Label_Item)context).getAlertwaiting2().show();
							try{
								new Thread(){
									public void run() {
										ListLabelDAO.moveLibrary(approvedoc, LoginActivity.database,onelabel.getId());
										((Library_Label_Item)context).getUiHandler2().post( new Runnable(){
											@Override
											public void run() {
												if(((Library_Label_Item)context).isUpdateRequired2()){
												}else{
													((Library_Label_Item)context).loadContentLstLable();
													((Library_Label_Item)context).getAlert_pop1().cancel();
													((Library_Label_Item)context).setFlagviewpop(0);
													((Library_Label_Item)context).getAlertwaiting2().cancel();		                               
												}
											}
										} );
									}
								}.start();
							}catch (Exception e) {}
						}
						
					});
					builder5.setNegativeButton(LoginActivity.lang.getString("cancel"), new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {

						}
					});
					AlertDialog alertDialog5 = builder5.create();
					alertDialog5.show();
					break;
				default:
					break;
				}
			}
		});
		return convertView;
	}

	public static class ViewHolder{
		LinearLayout lila_adapter_listlabel;
		ImageView imageView;
		TextView tvlabelname;
	}
}
